package cn.dxh.stagefinal.controller;

import cn.dxh.stagefinal.bean.Employee;
import cn.dxh.stagefinal.bean.Order;
import cn.dxh.stagefinal.config.PageConfig;
import cn.dxh.stagefinal.service.OrderService;
import cn.dxh.stagefinal.utils.ExcelUtils;
import cn.dxh.stagefinal.utils.OrderExcelUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpCookie;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/findOrders")
    public String getOrders(@RequestParam(defaultValue = "1") Integer pageindex, String
            ordernum, String source, String orderdate, String username, ModelMap modelMap, Integer state,HttpSession session){
        PageInfo<Order> pageInfo = orderService.findAll(pageindex, PageConfig.PAGE_SIZE, ordernum, source, orderdate, username,state);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("ordernum",ordernum);
        modelMap.addAttribute("source",source);
        modelMap.addAttribute("orderdate",orderdate);
        modelMap.addAttribute("username",username);
        modelMap.addAttribute("state",state);
        modelMap.addAttribute("pageindex",pageindex);
        modelMap.addAttribute("pages",pageInfo.getPages());
//        Employee role = orderService.getRole(loginName, password);
//        modelMap.addAttribute("empl",role);
        Employee currentUser = (Employee) session.getAttribute("currentUser");
        Employee employee = orderService.getRole(currentUser.geteId());
        modelMap.addAttribute("empl",employee);
        return "/order/orderlist";
    }

    @RequestMapping("/order/findone")
    public String getOne(Integer orderid,ModelMap modelMap){
        Order order = orderService.getOne(orderid);
        modelMap.addAttribute("order",order);
        return "/order/orderview";
    }

    @RequestMapping("/order/finddelivery")
    public String finddelivery(Integer orderid,ModelMap modelMap){
        List<Employee> employees= orderService.finddelivery();
        modelMap.addAttribute("employees",employees);
        modelMap.addAttribute("orderid",orderid);
        return "/order/deliverylist";
    }

    @RequestMapping("/order/choose")
    public void choose(String eName, Integer orderid, HttpServletResponse response) throws IOException {
        Integer choose = orderService.choose(eName, orderid);
        if (choose>0){
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<script>alert('派单成功');location.href='/findOrders'</script>");
        }
    }

    @RequestMapping("/order/sendgoods")
    public void sendgoods(Integer orderid,HttpServletResponse response) throws IOException {
        Integer sendgoods = orderService.sendgoods(orderid, 3);
        if (sendgoods>0){
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<script>alert('发货成功');location.href='/findOrders'</script>");
        }
    }

    @RequestMapping("/order/findsendlist")
    public String findsend(@RequestParam(defaultValue = "1") Integer pageindex, String
            ordernum, String source, String orderdate, String username, ModelMap modelMap,
            Integer[] orderstate,String deliveryman,HttpSession session,@RequestParam(defaultValue = "li1") String li) throws IOException {
        PageInfo<Order> pageInfo = orderService.findsend(pageindex, PageConfig.PAGE_SIZE, ordernum, source, orderdate, username, orderstate, deliveryman);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("ordernum",ordernum);
        modelMap.addAttribute("source",source);
        modelMap.addAttribute("orderdate",orderdate);
        modelMap.addAttribute("username",username);
        modelMap.addAttribute("orderstate",orderstate);
        modelMap.addAttribute("pageindex",pageindex);
        modelMap.addAttribute("pages",pageInfo.getPages());
        Employee currentUser = (Employee) session.getAttribute("currentUser");
        Employee employee = orderService.getRole(currentUser.geteId());
        modelMap.addAttribute("empl",employee);
        modelMap.addAttribute("li",li);
        return "/order/sendlist";
    }

    @RequestMapping("/order/onesend")
    public String onesend(Integer orderid,ModelMap modelMap){
        Order one = orderService.getOne(orderid);
        modelMap.addAttribute("order",one);
        return "/order/sendview";
    }

    @RequestMapping("/order/sendman")
    public String sendman(Integer[] orderids,ModelMap modelMap){
        List<Employee> employees= orderService.finddelivery();
        modelMap.addAttribute("employees",employees);
        modelMap.addAttribute("orderids",orderids);
        return "/order/sendmanlist";
    }

    @RequestMapping("/order/chooseids")
    public void chooseids(String eName, Integer[] orderids, HttpServletResponse response) throws IOException {
        Integer choose = orderService.chooseids(eName, orderids);
        if (choose>0){
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<script>alert('派单成功');location.href='/order/findsendlist'</script>");
        }
    }

    @RequestMapping("/order/exportorder")
    public void export(Integer[] orderids,HttpServletResponse response) throws IOException {
        List<Order> export = orderService.export();
        OrderExcelUtils.createFirst("订单信息");

        OrderExcelUtils.createOthers(export);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        FileOutputStream fos=new FileOutputStream("d:\\javaCreate\\" + "order" + (format.format(new Date())) + ".xls");
        OrderExcelUtils.export(fos);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("<script>alert('导出成功');location.href='/findOrders';</script>");
    }
}
